package com.echoesnet.echoesshortvideo.Activities;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.echoesnet.echoesshortvideo.Constant.EsvConstant;
import com.echoesnet.echoesshortvideo.Persistence.VideoPathBean;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.ViewModels.VideoSelectModel;
import com.echoesnet.echoesshortvideo.views.adapters.VideoSelectAdapter;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Copyright (C) 2017 在线回声（天津）科技发展有限公司
 * 在线回声完全享有此软件的著作权，违者必究
 *
 * @author an
 * @version 1.0
 * @modifier
 * @createDate 2018/4/23 0023
 * @description
 */
public class VideoSelectAct extends BaseActivity
{
    private final static String TAG = VideoSelectAct.class.getSimpleName();

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private VideoSelectModel videoSelectModel;
    private List<VideoPathBean> videoList;
    private VideoSelectAdapter videoSelectAdapter;
    private Activity mAct;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mAct = this;
        setContentView(R.layout.act_video_select);
        ButterKnife.bind(this);
        //init recyclerView
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3, LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(gridLayoutManager);
        videoList = new ArrayList<>();
        videoSelectAdapter = new VideoSelectAdapter(this,videoList,getItemImageWidth());
        recyclerView.setAdapter(videoSelectAdapter);
        videoSelectAdapter.setVideoSelectClick(videoPathBean -> {
            String url = videoPathBean.getVideoPath();
            Logger.t(TAG).d("url>>>>>>>>" + url);
            Intent intent = new Intent(mAct,CutTimeActivity.class);
            intent.putExtra("path",url);
            startActivityForResult(intent,EsvConstant.OPEN_CUT_VIDEO);
        });

        videoSelectModel = ViewModelProviders.of(this).get(VideoSelectModel.class);
        videoSelectModel.getVideoList().observe(this, new Observer<List<VideoPathBean>>()
        {
            @Override
            public void onChanged(@Nullable List<VideoPathBean> videoPathBeans)
            {
                videoList.clear();
                if (videoPathBeans != null)
                 videoList.addAll(videoPathBeans);
                videoSelectAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick({R.id.img_back})
    public void onViewClick(View view){
        switch (view.getId())
        {
          case R.id.img_back:
              finish();
            break;
        }
    }

    /**
     * 根据屏幕宽度与密度计算显示的列数， 最少为三列
     *
     * @return
     */
    private int getNumColnums() {
        int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi;
        return cols < 3 ? 3 : cols;
    }

    /**
     * 获取Item宽度
     *
     * @return
     */
    private int getItemImageWidth() {
        int cols = getNumColnums();
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        int columnSpace = getResources().getDimensionPixelOffset(R.dimen.space_size);
        return (screenWidth - columnSpace * (cols - 1)) / cols;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == EsvConstant.OPEN_CUT_VIDEO && resultCode == RESULT_OK)
        {
            finish();
        }
    }
}
